from rest_framework import serializers
from book.models import BookInfo,PeopleInfo


class BookInfoSerializer(serializers.Serializer):
    id = serializers.IntegerField(label='id', read_only=True)
    name = serializers.CharField(label='名称',max_length=50)
    pub_date = serializers.DateField(label='发布日期', required=False)
    readcount = serializers.IntegerField(label='阅读量', required=False)
    commentcount = serializers.IntegerField(label='评论量', required=False)
    # image = serializers.ImageField(label='图片')

    # def validate_readcount(self, value):
    #     if value < 0:
    #         raise serializers.ValidationError('阅读量不能为负数')
    #     return value
    #
    # def validate_commentcount(self, data):
    #     if data < 0:
    #         raise serializers.ValidationError('评论量不能为负数')
    #     return data

    def validate(self, attrs):
        readcount = attrs.get('readcount')
        commentcount = attrs.get('commentcount')
        if readcount < 0:
            raise serializers.ValidationError('阅读量不能为负数')
        if commentcount < 0:
            raise serializers.ValidationError('评论量不能为负数')
        if readcount < commentcount:
            raise serializers.ValidationError('评论量不能大于阅读量')
        return attrs

    def create(self, validated_data):
        return BookInfo.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.pub_date = validated_data.get('pub_date', instance.pub_date)
        instance.readcount = validated_data.get('readcount', instance.readcount)
        instance.commentcount = validated_data.get('commentcount', instance.commentcount)
        instance.save()
        return instance


# class PeopleInfoSerializer(serializers.Serializer):
#     GENDER_CHOICES = (
#         (0, 'male'),
#         (1, 'female')
#     )
#     id = serializers.IntegerField(label='id')
#     name = serializers.CharField(label='名称')
#     gender = serializers.ChoiceField(choices=GENDER_CHOICES, label='性别')
#     description = serializers.CharField(label='描述信息')
#     # 关联字段序列化方法1：IntegerField——序列化输出key为：外键_id
#     # book_id = serializers.IntegerField(label='书籍id')
#
#     # 关联字段序列化方法2：PrimaryKeyRelatedField——序列化输出key为：模型类中的属性名
#     # book = serializers.PrimaryKeyRelatedField(label='书籍id', read_only=True或者查询集)
#     # 包含read_only = True参数时，该字段将不能用作反序列化使用
#     # 包含queryset参数时，将被用作反序列化时参数校验使用
#     # book = serializers.PrimaryKeyRelatedField(label='书籍id', queryset=BookInfo.objects.all())
#     # book = serializers.PrimaryKeyRelatedField(label='书籍id', read_only=True)
#
#     # 关联字段序列化方法3：StringRelatedField——序列化输出key为：模型类中的属性名，值为BookInfo模型类中的__str__方法的结果
#     # 其中StringRelatedField关联的是BookInfo模型类中的__str__方法
#     # book = serializers.StringRelatedField(label='图书')
#
#     # 关联字段序列化方法4：使用关联对象的序列化器
#     book = BookInfoSerializer()


class PeopleInfoSerializer(serializers.ModelSerializer):

    class Meta:
        model = PeopleInfo
        fields = '__all__'
        extra_kwargs = {
            'name': {'min_length':2, 'max_length':10}
        }